package test;

import java.io.*;
import java.net.Socket;
import java.util.Arrays;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 模拟客户端
 * <P> Date: 2022/2/28:16:19
 * <P> Description:
 */

public class MyJedisClient {
    private static Socket socket;

    static {
        try {
            socket = new Socket("localhost", 6379);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public MyJedisClient() {
    }

    //    *3
//    $3
//            SET
//    $5
//            mykey
//    $4
//            test
    public void set(String key, String value) {
        StringBuffer sb = new StringBuffer();

        sb.append("*3").append("\r\n");
        sb.append("$3").append("\r\n");
        sb.append("SET").append("\r\n");
        sb.append("$").append(key.length()).append("\r\n");
        sb.append(key).append("\r\n");
        sb.append("$").append(value.length()).append("\r\n");
        sb.append(value).append("\r\n");

        OutputStream os = null;
        try {
            os = socket.getOutputStream();
            byte[] bytes = sb.toString().getBytes();
            os.write(bytes);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

//    *2
//    $3
//            GET
//    $5
//            mykey

    /**
     * <P>Author ideaAdmin
     * <P>TODO: 发送命令 获得redis返回的数据
     * <P>Date 17:50 2022/2/28
     * <P>* @param key
     * <P>* @return java.lang.String
     **/
    public String get(String key) {
        StringBuffer sb = new StringBuffer();

        sb.append("*2").append("\r\n");
        sb.append("$3").append("\r\n");
        sb.append("GET").append("\r\n");
        sb.append("$").append(key.length()).append("\r\n");
        sb.append(key).append("\r\n");

        OutputStream os = null;
        InputStream is = null;
        try {
            os = socket.getOutputStream();
            byte[] bytes = sb.toString().getBytes();
            os.write(bytes);

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            int read = 0;
            byte[] bytes1 = new byte[1024];
//            StringBuilder sb1 = new StringBuilder();
            StringBuffer sb2 = new StringBuffer();
            String s = new String();
            while ((s = br.readLine()) != null) {
                sb2.append(new StringBuffer(s));
                System.out.println(s);

                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return sb2.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
}
